home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / PGM_TOOL / PREVIEW / MISC.PAS < prev    next >
Pascal/Delphi Source File  |  1995-11-12  |  10KB  |  427 lines

  1. Unit Misc;
  2.  
  3. Interface
  4.  
  5. uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  6.      Forms, Dialogs, StdCtrls, Buttons, wYNform;
  7.  
  8. Const MaxPars=20;
  9.       UPARROW=38; { in KeyDown events, GetUp(),GetDown(),GetEsc() }
  10.       DNARROW=40;
  11.       ESCKEY=27;
  12.       RETKEY=13;
  13.       RETCHAR=#13;  { in KeyPress events, GetRet() }
  14.       NULLCHAR=#0;
  15.       DNCHAR=#40;
  16.       UPCHAR=#38;
  17.       ESCCHAR=#27;
  18.  
  19. Type
  20.   String135=String[137];
  21.   String30=String[31];
  22.   GenVars=class(TObject)
  23.       public
  24.       { used to store BluePrint Images }
  25.       User:string[10];
  26.         FullBP,TinyBP,PrintBP:TBitMap;
  27.       InBluePrint:boolean; { only allow one open at a time }
  28.             procedure AddWin(astr:string;aform:Tform);
  29.       procedure ReleaseWin(aform:Tform);
  30.     end;
  31.  
  32. procedure StartMisc;
  33. procedure StopMisc;
  34. function  Pin(str1,instr2:string):boolean;  { pos()>0 }
  35. function  uPin(str1,instr2:string):boolean;  { pos()>0 }
  36. function  YesNoBox(text:string):boolean;
  37. function  iifs(abool:boolean;ret1,ret2:string):string;
  38. function  Empty(aStr:String):Boolean;
  39. function  ProcInt(nval:string):integer;
  40. function  ProcDbl(nval:string):double;
  41. procedure split(orgline,pchar:string;
  42.             var resarr:array of string135;var rescnt:integer);
  43. function  Trim(aStr:String):String;   { trim off trailing spaces }
  44. function  pp(var anInt:integer):integer;  { ii:=ii+1  ==>  pp(ii) }
  45. function  iifi(abool:boolean;ret1,ret2:integer):integer;
  46. function  lTrim(aStr:String):String;   { trim off leading spaces }
  47. procedure OKbox(sText:String);
  48. function  GetRet(var aChar:char):boolean;
  49. function  PadR(aStr:String;InWidth:Integer):String; { left justify in width }
  50. function  Space(EmptySize:Integer):String;  { return string of spaces }
  51. procedure MouseWait;
  52. procedure MouseGo;
  53. procedure CenterHoriz(aform:Tform);
  54. function  Upper(aStr:string):string;
  55. function  SubStr(astr:string;fromm,too:integer):string;
  56. function  unsplit(var arr1:array of string135;delim:string;acnt:integer):string;
  57. function  StrD(aDbl:double;ToPlaces:integer):string;
  58. function  Str(aDbl:double;width,decs:integer):string;
  59. function  StrI(aInt:longint;width:integer):string;
  60. function  ComPath(aFile:string):string;
  61. function  PadL(aStr:String;InWidth:Integer):String; { right justify in width }
  62. procedure DoEvents;
  63. procedure DoEvents2;
  64. var Gen:GenVars;
  65.  
  66. Implementation
  67.  
  68. function  ComPath(aFile:string):string;
  69. begin
  70.   result:=aFile;
  71. end;
  72.  
  73. function PadL(aStr:String;InWidth:Integer):String; { right justify in width }
  74. var ll:integer;
  75. begin
  76.     ll:=length(aStr);
  77.     if ll>=InWidth then Result:=copy(aStr,1,Inwidth)  { truncate }
  78.     else Result:=space(InWidth-ll)+aStr;
  79. end;
  80.  
  81. procedure DoEvents;
  82. begin
  83.     Application.ProcessMessages;
  84. end;
  85.  
  86. procedure DoEvents2;
  87. begin
  88.     Application.ProcessMessages;
  89. end;
  90.  
  91. function str(aDbl:double;width,decs:integer):string;
  92. var nines,before,after:string[30];
  93.     ii:integer;
  94. begin
  95.     Result:=format('%*.*f',[width,decs,aDbl]);
  96. end;
  97.  
  98. function StrI(aInt:longint;width:integer):string;
  99. begin
  100.   result:=padl(inttostr(aInt),width);
  101. end;
  102.  
  103. function  StrD(aDbl:double;ToPlaces:integer):string;
  104. var InWidth:integer;
  105. begin
  106.   InWidth:=8;
  107.     if ToPlaces>0 then InWidth:=8+1+ToPlaces;
  108.   Result:=ltrim(str(aDbl,InWidth,ToPlaces));
  109. end;
  110.  
  111. function unsplit(var arr1:array of string135;delim:string;acnt:integer):string;
  112. { array may be 1 based, but when passed in it becomes 0 based }
  113. var ii,jj,pp:integer;
  114.     tt:string;
  115. begin
  116.   tt:='';
  117.   if acnt=1 then begin
  118.     tt:=arr1[0];
  119.   End;
  120.   if acnt>1 then begin
  121.     for ii:=0 to acnt-2 do begin
  122.       tt:=tt+arr1[ii]+delim;
  123.     End;
  124.     tt:=tt+arr1[acnt-1];
  125.   End;
  126.   Result:=tt;
  127. end;
  128.  
  129. function  SubStr(astr:string;fromm,too:integer):string;
  130. begin
  131.   result:=copy(astr,fromm,too);
  132. end;
  133.  
  134. procedure GenVars.AddWin(astr:string;aform:Tform);
  135. begin
  136.   { do nothing }
  137. end;
  138.  
  139. procedure GenVars.ReleaseWin(aform:Tform);
  140. begin
  141.   { do nothing }
  142. end;
  143.  
  144. function Upper(aStr:string):string;begin
  145.   result:=uppercase(aStr);
  146. end;
  147.  
  148. procedure CenterHoriz(aform:Tform);
  149. var ii:integer;
  150. begin
  151.   ii:=(screen.width-aform.width-8) div 2;
  152.   if ii<0 then aform.left:=0 else aform.left:=ii;
  153. end;
  154.  
  155. procedure MouseWait;
  156. begin
  157.   Screen.Cursor:=crHourGlass;
  158.   Application.ProcessMessages;
  159. end;
  160.  
  161. procedure MouseGo;
  162. begin
  163.   Screen.Cursor:=crDefault;
  164.   Application.ProcessMessages;
  165. end;
  166.  
  167. function Space(EmptySize:Integer):String;  { return string of spaces }
  168. var tt,tt2:string;
  169.         ii:integer;
  170. begin
  171.     tt:='                              ';
  172.     tt2:='';
  173.     for ii:=1 to 5 do tt2:=tt2+tt;
  174.     ii:=length(tt2);
  175.     Result:=copy(tt2,1,EmptySize);
  176. end;
  177.  
  178. function PadR(aStr:String;InWidth:Integer):String; { left justify in width }
  179. var ll:integer;
  180. begin
  181.     ll:=length(aStr);
  182.     if ll>=InWidth then Result:=copy(aStr,1,Inwidth)  { truncate }
  183.     else Result:=aStr+space(InWidth-ll);
  184. end;
  185.  
  186. function GetRet(var aChar:char):boolean;
  187. begin
  188.   if aChar=escchar then aChar:=nullchar;
  189.   if aChar=retchar then begin
  190.       aChar:=nullchar;
  191.         Result:=true;
  192.     end else Result:=false;
  193. end;
  194.  
  195. procedure OKbox(sText:String);
  196. var tyn:TYNform;
  197. begin
  198.   tyn:=TYNform.create(application);
  199.   tyn.setup(1,'Job Cost',stext);
  200.   tyn.showmodal;
  201. end;
  202.  
  203. function lTrim(aStr:String):String;   { trim off trailing spaces }
  204. var ii,kk,ll:integer;
  205. begin
  206.     ll:=length(aStr);
  207.     Result:=aStr;
  208.     if ll>0 then begin
  209.         kk:=0;
  210.         for ii:=1 to ll do begin
  211.             if aStr[ii]<>#32 then begin
  212.                 kk:=ii;
  213.                 break;
  214.             end;
  215.         end;
  216.         if kk>0 then Result:=copy(astr,kk,254)
  217.         else Result:='';
  218.     end;
  219. end;
  220.  
  221. function iifi(abool:boolean;ret1,ret2:integer):integer;
  222. {  iif() when params are integer's }
  223. begin
  224.   if abool then result:=ret1 else result:=ret2;
  225. end;
  226.  
  227. function pp(var anInt:integer):integer;  { ii:=ii+1  ==>  pp(ii) }
  228. begin
  229.   result:=anInt;  { usage:  lp.p(line++,5,'Hi') -> lp.p(pp(line),5,'Hi') } 
  230.     anInt:=anInt+1;
  231. end;
  232.  
  233. function  Pin(str1,instr2:string):boolean;  { pos()>0 }
  234. begin
  235.   result:=(pos(str1,instr2)>0);
  236. end;
  237.  
  238. function  uPin(str1,instr2:string):boolean;  { pos()>0 }
  239. begin
  240.   result:=(pos(upper(str1),upper(instr2))>0);
  241. end;
  242.  
  243. function  YesNoBox(text:string):boolean;
  244. var ret:integer;
  245.     tyn:TYNform;
  246. begin
  247.   tyn:=TYNform.create(application);
  248.   tyn.setup(2,'Job Cost',text);
  249.   ret:=tyn.showmodal;
  250.     Result:=(ret=mrYES);
  251. end;
  252.  
  253. function iifs(abool:boolean;ret1,ret2:string):string;
  254. {  iif() when params are string's }
  255. begin
  256.   if abool then result:=ret1 else result:=ret2;
  257. end;
  258.  
  259. procedure StartMisc;
  260. begin
  261.   Gen:=genvars.create;
  262.   Gen.User:='BRAD ';
  263.   Gen.FullBP:=tbitmap.create;
  264.   Gen.TinyBP:=tbitmap.create;
  265.   Gen.PrintBP:=tbitmap.create;
  266. end;
  267.  
  268. procedure StopMisc;
  269. begin
  270.   Gen.free;
  271.   Gen.FullBP.free;
  272.   Gen.TinyBP.free;
  273.   Gen.PrintBP.free;
  274. end;
  275.  
  276. function Empty(aStr:String):Boolean;
  277. var ii,ll:integer;
  278.         res:boolean;
  279. begin
  280.     if length(aStr)=0 then res:=true
  281.     else
  282.     begin
  283.         ll:=length(aStr);
  284.         if (ll=8) or (ll=10) then { check for date? }
  285.         begin
  286.             if (aStr[3]=#47) and (aStr[6]=#47) then { chars 3 and 6 are "/" }
  287.             begin
  288.                 ll:=2; { only need to test first 2 chars of dates }
  289.                 if pos('00',aStr)=1 then ll:=0  { ignore '00/00/00' }
  290.             end;
  291.         end;
  292.         res:=True;
  293.         if ll>0 then begin
  294.             for ii:=1 to ll do begin
  295.                 if aStr[ii]<>#32 then begin
  296.                     res:=False;
  297.                     break;
  298.                 end;
  299.             end;
  300.         end;
  301.     end;
  302.     Result:=res;
  303. end;
  304.  
  305. function  ProcInt(nval:string):integer;
  306. var tdbl:double;
  307. begin
  308.   tdbl:=ProcDbl(nval);
  309.   result:=StrToInt(format('%8.0f',[tdbl]));
  310. end;
  311.  
  312. function procdbl(nval:string):double;
  313. var decs,prnum,jj:double;
  314.         ii:integer;
  315.         ist:string[30];
  316.         pastdec,isminus:boolean;
  317. begin
  318.     prnum:=0.00;
  319.     pastdec:=False;
  320.     isminus:=False;
  321.     decs:=1.0;
  322.     if not empty(nval) then begin
  323.         for ii:=1 to length(nval) do begin
  324.             ist:=Copy(nval,ii,1);
  325.             if ist='-' then begin
  326.                 isminus:=True;
  327.             End;
  328.             if ist='.' then begin
  329.                 pastdec:=True;
  330.             End Else
  331.             Begin
  332.                 if (ist >= '0') And (ist <= '9') then begin
  333.                     jj:=StrToFloat(ist);
  334.                     prnum := prnum * 10.0;
  335.                     prnum := prnum + jj;
  336.                     if pastdec then begin
  337.                         decs:=decs / 10.0;
  338.                     End;
  339.                 End;
  340.             End;
  341.         End;
  342.         if isminus then begin
  343.             prnum:=prnum * decs * -1;
  344.         End Else
  345.         Begin
  346.             prnum:=prnum * decs;
  347.         End;
  348.         if Not pastdec then begin
  349.             prnum:=int(prnum);
  350.         End;
  351.     end;
  352.     Result:=prnum;
  353. end;
  354.  
  355. procedure split(orgline,pchar:string;
  356.   var resarr:array of string135;var rescnt:integer);
  357. var aline:string;
  358.     ii,jj,kk,acnt,plen:integer;
  359.         ats:array [1..80] of integer;
  360. begin
  361.   for ii:=0 to high(resarr) do resarr[ii]:='';
  362.   rescnt:=0;
  363.   for ii:=1 to 80 do ats[ii]:=0;
  364.   aline:=orgline;
  365.   jj:=length(aline);
  366.   plen:=length(pchar);
  367.   if jj>0 then begin
  368.     rescnt:=1;
  369.     ats[rescnt]:=0;
  370.     for ii:=1 to jj do begin
  371.       if Copy(aline,ii,plen)=pchar then begin
  372.         rescnt:=rescnt+1;
  373.         ats[rescnt]:=ii;
  374.       End;
  375.     End;
  376.     ats[rescnt+1]:=jj;
  377.     if rescnt=1 then begin
  378.       resarr[0]:=aline;
  379.     End Else
  380.     Begin
  381.       for ii:=1 to rescnt do begin
  382.         if ii=1 then begin
  383.           kk:=ats[ii+1]-ats[ii]-1;
  384.           if kk>0 then begin
  385.             resarr[ii-1]:=Copy(aline,1,kk);
  386.           End;
  387.         end else
  388.         if ii=rescnt then begin
  389.           kk:=ats[ii+1]-ats[ii]-plen+1;
  390.           if kk>0 then begin
  391.             resarr[ii-1]:=Copy(aline,ats[ii]+plen,kk);
  392.           End;
  393.         end Else
  394.         begin
  395.           kk:=ats[ii+1]-ats[ii]-plen;
  396.           if kk>0 then begin
  397.             resarr[ii-1]:=Copy(aline,ats[ii]+plen,kk);
  398.           End;
  399.         End;
  400.       End;
  401.     End;
  402.   End;
  403. end;
  404.  
  405. function Trim(aStr:String):String;   { trim off trailing spaces }
  406. var ii,kk,ll:integer;
  407. begin
  408.     ll:=length(aStr);
  409.     Result:=aStr;
  410.     if ll>0 then begin
  411.         kk:=0;
  412.         for ii:=ll downto 1 do begin
  413.             if aStr[ii]<>#32 then begin
  414.                 kk:=ii;
  415.                 break;
  416.             end;
  417.         end;
  418.         if kk>0 then Result:=copy(astr,1,kk)
  419.         else Result:='';
  420.     end;
  421. end;
  422.  
  423.  
  424.  
  425.  
  426. end.
  427.